﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections;
using UtilsORM.MySqlData;
using UtilsORM;
using NPOI.POIFS.Properties;

namespace ServiceWeb.WebAdmin
{
    public class ServiceSystemMenu
    {
        public DataTable GetList(string Menu_Parent, MagerLoginInfo magerLoginInfo)
        {

            string sqlwhere = "  and rm_r_guid in(select ur_r_guid from bs_manage_user_role where ur_cu_guid='" + magerLoginInfo.user_guid + "')";
            if (Menu_Parent == null)
            {
                sqlwhere += " and menu_parent = menu_guid ";
            }
            else
            {
                sqlwhere += " and menu_parent != menu_Guid and Menu_Parent='" + Menu_Parent + "' ";
            }
            string sql = "select distinct menu_grade,menu_Order,menu_Guid,menu_parent,menu_Name,menu_LinkUrl,menu_type,menu_iconCss,menu_index from bs_manage_system_menu inner join bs_manage_role_menu on rm_menu_Guid=menu_guid  where menu_status=0 and menu_type=0 " + sqlwhere + " order by menu_Order ";
            DataTable dt = MySqlYihongFileServer.GetDataTable(sql);
            return dt;
        }

        public List<RespMenu> GetListAll(string Menu_Parent, MagerLoginInfo magerLoginInfo)
        {
            List<RespMenu> respMenus = new List<RespMenu>();

            DataTable dtAll = new DataTable();
            DataTable dt = GetList( Menu_Parent,  magerLoginInfo);
            foreach(DataRow dr  in dt.Rows)
            {

                RespMenu respMenu = new RespMenu();
                respMenu.menuGrade = int.Parse(dr["menu_grade"].ToString());
                respMenu.menuOrder = int.Parse(dr["menu_Order"].ToString());
                respMenu.menuGuid = dr["menu_Guid"].ToString();
                respMenu.menuParent = dr["menu_parent"].ToString();
                respMenu.menuName = dr["menu_Name"].ToString();
                respMenu.menuLinkUrl = dr["menu_LinkUrl"].ToString();
                respMenu.menuType = int.Parse(dr["menu_type"].ToString());
                respMenu.menuIconCss = dr["menu_iconCss"].ToString();
                respMenu.menuIndex = int.Parse(dr["menu_index"].ToString());

                List<RespMenu> respMenusChildList = new List<RespMenu>();
                DataTable dtchild = GetList(dr["menu_Guid"].ToString(),  magerLoginInfo);
                foreach (DataRow drChild in dtchild.Rows)
                {

                    RespMenu respMenuChild = new RespMenu();
                    respMenuChild.menuGrade = int.Parse(drChild["menu_grade"].ToString());
                    respMenuChild.menuOrder = int.Parse(drChild["menu_Order"].ToString());
                    respMenuChild.menuGuid = drChild["menu_Guid"].ToString();
                    respMenuChild.menuParent = drChild["menu_parent"].ToString();
                    respMenuChild.menuName = drChild["menu_Name"].ToString();
                    respMenuChild.menuLinkUrl = drChild["menu_LinkUrl"].ToString();
                    respMenuChild.menuType = int.Parse(drChild["menu_type"].ToString());
                    respMenuChild.menuIconCss = drChild["menu_iconCss"].ToString();
                    respMenuChild.menuIndex = int.Parse(drChild["menu_index"].ToString());
                    respMenusChildList.Add(respMenuChild);
                }
                respMenu.children = respMenusChildList;
                respMenus.Add(respMenu);
            }
            return respMenus;
        }
        public string GetMenuName(string Menu_Guid)
        {

            return new EFYihongIotContext().bs_manage_system_menu.Where(c => c.menu_guid == Menu_Guid && c.menu_status == 0).Select(c => c.menu_name).FirstOrDefault();

        }

    }
}
